// pages/activateClass/activateClass.js
var userModel = require('../../models/user.js')
var klassModel = require('../../models/klass.js')
var request = require('../../operation/operation.js')
var bxt = require('../../utils/bxt.js') 

var wxGroupUid = null, wxGroupName = null

Page({

  /**
   * 页面的初始数据
   */
  data: {
    school:{},
    klass:{},
    user:{},
    disabled: false, // 确定按钮可用/不可用控制
    loading: false, // 通信加载提示
    autoBind:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let param = getApp().globalData.param,that = this
    
    let autoBind = false
    if (options.autoBind) {
      autoBind = true
    }

    
    // 获取编码后的群组信息
    wx.getShareInfo({
      shareTicket: param.appOptions.shareTicket,
      success(res) {
        wxGroupUid = res.encryptedData
        wxGroupName = res.iv

        that.getWxGroupUid()
      }
    })
    // 初始化界面
    let user = userModel.getCurrentUser()
    this.setData({
      user:user,
      // school: param.notice.klass.school,
      klass: klassModel.getKlassUIData(param.notice.klass),
      autoBind: autoBind
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  onNext:function(event) {
    if (0 == event.detail.value.password.trim().length) {
      wx.showModal({
        title: '提示',
        content: '请输入激活码',
        showCancel:false
      })

      return
    }

    this.setButtonLoading(true)
    var that = this  
    var params = { 'wxGroupUid': wxGroupUid, 'wxGroupName': wxGroupName, 'password': event.detail.value.password.trim(),'encrypted':true}
    request.putRequest('/klasses/' + this.data.klass.sid, params, true)
      .then(res => {
        that.setButtonLoading(false)
        var data = res.data;
        if (request.SUCCESSED == data.status) {

          // 更新用户班级状态
          getApp().updateUserInfo(function (userInfo) {
            if (userInfo) {
              getApp().notificationCenter.post(bxt.ACTIVATE_KCLASS_MESSAGE)
            }
          })

          getApp().autoBack('绑定成功')
        }else {
          wx.showToast({
            title: data.msg,
            icon: 'none'
          })
        }

      }).catch(e => {
        that.setButtonLoading(false)
      })
  },

   getWxGroupUid:function() {
       var params = { 'encryptedData': wxGroupUid, 'iv': wxGroupName},that= this
       request.postRequest('/get-wx-group-uid', params, true)
           .then(res => {
               var data = res.data;
               if (request.SUCCESSED == data.status) {
                  that.setData({
                      wxGroupUid:data.object.wxGroupUid
                  })
               }
           }).catch(e => {
           that.setButtonLoading(false)
       })
   },

   /**
   * 设置按钮状态
   */
  setButtonLoading: function (loading) {
    this.setData({
      disabled: loading,
      loading: loading
    })
  },
})